
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Data;
using System.Data.Odbc;

namespace Swaf.DataAccess
{
	/// <summary>
	/// Summary description for ODBCHelper.
	/// </summary>
	public class ODBCHelper : IAdoNetHelper
	{
		public ODBCHelper()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region IAdoNetHelper Members

		public void addCommandParam(IApplication app, IDbCommand cmd, string name, string type, int size, ParameterDirection dir)
		{
			if (cmd is OdbcCommand)
			{
				OdbcCommand oledbcmd = (OdbcCommand) cmd;
				IDbDataParameter p = oledbcmd.Parameters.Add("@" + name, (OdbcType) Enum.Parse(typeof(OdbcType), type, true), size);

				if (dir != ParameterDirection.Input)
				{
					p.Direction = dir;
				}
			}
		}

		public void addCommandParam(IApplication app, IDbCommand cmd, string name, string type, int size, ParameterDirection dir, object val)
		{
			if (cmd is OdbcCommand)
			{
				OdbcCommand oledbcmd = (OdbcCommand) cmd;
				IDbDataParameter p = oledbcmd.Parameters.Add("@" + name, (OdbcType) Enum.Parse(typeof(OdbcType), type, true), size);
				p.Value = (val == null)?DBNull.Value:val;

				if (dir != ParameterDirection.Input)
				{
					p.Direction = dir;
				}
			}
		}

		public object getParamValue(IDataParameterCollection parameters, string paramName)
		{
			object paramValue = ((OdbcParameter)((parameters[@"@" + paramName]))).Value;

			if (paramValue.GetType() == typeof(System.Guid))
			{
				paramValue = new BGuid.Guid((System.Guid) paramValue);
			}

			return paramValue;
		}

		public bool allowParamOutputMapping(IDataParameterCollection parameters, string paramName)
		{
			return true;
		}

		#endregion
	}
}
